<?php
if (! defined ( 'BASEPATH' )) {
	exit ( 'no direct script access allowed!' );
}
/**
 * 好友推荐模块操作
 * Enter description here ...
 * @author 庆安
 *
 */
class photowall_model extends CI_Model {
	function __construct() {
		parent::__construct ();
		
	}
	/**
	 * 添加照片墙 个人 或团队
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 int 类型 0 失败 大于 0 成功 
	 */
	function insertFhotowall($data) {
		
		$this->db->insert ( 'wanwan_enum_photowall', $data );
		return $this->db->insert_id ();
	}
	function queryPersonalYearbook($data, $limit, $offset) {
		$this->db->order_by ( "createTime", "desc" );
		$rs = $this->db->get_where ( 'wanwan_enum_photowall', $data,$offset , $limit - 1 );
		return $rs->result_array ();
	}
	
	/**
	 * 更新照片墙信息
	 * Enter description here ...
	 * @param  $data Array
	 * 返回 int 类型 0 失败  大于 0 成功
	 */
	function updatePhotowall($data) {
		$this->db->where ( 'photowallId', 'photowallId' );
		$this->db->update ( 'wanwan_enum_photowall', $this->__deleteKey ( 'photowallId', $data ) );
	
	}
	
	//获取收藏的照片
	function listCollectImg($data) {
		$userId = $data['userId'];
		$queryString = "SELECT * FROM wanwan_enum_collection WHERE userId = $userId";
		$rs = $this->db->query($queryString);
		return $rs->result_array();
	}
	
	//获取好友近期照片
	function listFriendsImg($data) {
		$userId = $data['userId'];
		$queryString = " SELECT * FROM wanwan_enum_photowall WHERE userId IN 
		(SELECT userId FROM wanwan_enum_usertoteam WHERE teamId IN(SELECT teamId FROM wanwan_enum_usertoteam WHERE userId= $userId ))
		ORDER BY createTime desc"; 
		$rs = $this->db->query($queryString);
		return $rs->result_array();
	}
	
	//获取上传的照片
	function listUploadImg($data, $beginNumber=-1, $pageSize=55) {
		$limit =($beginNumber==-1)?"":"limit $beginNumber,$pageSize";
		$userId = $data ['userId'];
		$sql = "SELECT id,userId,imgSrc,createTime,description,'dynamic' type FROM wanwan_enum_userdynamic WHERE userId=$userId  AND TYPE='image' 
			UNION ALL SELECT id,userId,imgSrc,createTime,description,'photowall' type FROM wanwan_enum_photowall WHERE userId=$userId order by createTime desc $limit";
		$rs = $this->db->query ( $sql )->result_array ();
		foreach ( $rs as $key => $row ) {
			if ($row['type']=='dynamic') {
				$arrayImg= explode ( ",", $row['imgSrc'] );
				unset($rs[$key]);
				foreach ( $arrayImg as $val ) {
					if (! empty ( $val )) {
						$row ['imgSrc'] = $val;
						array_push($rs,$row);
					}
				}
			}
		}
		return $rs;
	}
	function addPhotowall($data) {
		log_message("info","photo add".json_encode($data));
		print_r($data);
		$postArray = array ();
		$errorMsg = "图片添加失败";
		$successMsg = "";
		//log_message("info",json_encode($_FILES));
		$this->load->model ( "util/up_down_model" );
		$array = $this->up_down_model->upLoadImage ('Filedata');
		if (intval ( $array ['type'] ) == 1) {
			$postArray ['createTime'] = date ( 'Y-m-d H:i:s' );
			$postArray ['imgSrc'] = $array ['path'];
			$postArray ['description'] = $data ['description'];
			$postArray['userId']=$data["userId"];
			$int = $this->insertFhotowall ( $postArray );
			if (! empty ( $int )) {
				$successMsg = "图片添加成功";
			}
		}
		return array ('msg' => $successMsg . $errorMsg . "." );
	}
}